<template>
  <div class="container">
    <div class="main">
      <div class="pay-title">
        支付总金额
        <span class="pay-price">￥ {{ price }}</span>
      </div>
      <div class="pay-main">
        <h4>微信支付--</h4>
        <!-- 测试666 -->
        <!-- 777 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          class="pay-qrcode"
        >
          <div class="qrcode">
            <!-- 二维码 -->
            <canvas id="qrcode-stage"></canvas>
            <p>请使用微信扫一扫</p>
            <p>扫描二维码支付</p>
          </div>
          <div class="pay-example">
            <img src="http://157.122.54.189:9093/images/wx-sweep2.jpg" alt />
          </div>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import QRCode from "qrcode";
export default {
  data() {
    return {
      price: null,
      payInfo: null,
      timer: null,
    };
  },
  watch: {
    //监听 // 1. 如果是别的页面进来, vuex 本来就有, 第一次执行就得出结果,
    // 2. 如果是刷新, 一进来会执行一次, 发现没数据, 不会执行,发送请求需要token,所以要监听,并判断
    "$store.state.user.userInfo.token": {
      handler: function () {
        //监听了$store.state.user.userInfo.token有变化还要判断vuex有token才发请求
        if (this.$store.state.user.userInfo.token) {
          this.$axios({
            url: "/airorders/" + this.$route.query.id,
            headers: {
              Authorization: "Bearer " + this.$store.state.user.userInfo.token,
            },
          }).then((res) => {
            console.log("这是付款", res);
            var { payInfo, price } = res.data;
            this.price = price;
            this.payInfo = payInfo;
            var stage = document.querySelector("#qrcode-stage");
            QRCode.toCanvas(stage, payInfo.code_url, { width: 200 });
            this.checkpay();
          });
        }
      },
      // 页面进来时自动执行一次
      immediate: true,
    },
  },

  methods: {
    checkpay() {
      this.$axios({
        url: "/airorders/checkpay",
        headers: {
          Authorization: "Bearer " + this.$store.state.user.userInfo.token,
        },
        method: "post",
        data: {
          id: this.$route.query.id,
          nonce_str: this.price,
          out_trade_no: this.payInfo.order_no,
        },
      }).then((res) => {
        console.log(res.data.statusTxt);
        //支付状态
        if (res.data.trade_state == "NOTPAY") {
          this.timer = setTimeout(() => {
            this.checkpay();
          }, 2000);
        } else if (res.data.trade_state == "SUCCESS") {
          this.$message.success("支付成功");
        } else if (res.data.trade_state == "REVOKED") {
          this.$message.success("已撤销");
        } else if (res.data.trade_state == "CLOSED") {
          this.$message.warning("已关闭");
        } else if (res.data.trade_state == "PAYERROR") {
          this.$message.error("支付失败");
        } else if (res.data.trade_state == "USERPAYING") {
          this.$message.success("用户支付中");
        } else if (res.data.trade_state == "REFUND") {
          this.$message.success("转入退款");
        }
      });
    },
  },

  //生命周期,页面摧毁后执行,关闭定时器
  destroyed() {
    clearTimeout(this.timer);
  },
};
</script>

<style scoped lang="less">
.container {
  background: #f5f5f5;
  padding: 30px 0;

  .main {
    width: 1000px;
    margin: 0 auto;

    .pay-title {
      text-align: right;
      span {
        font-size: 28px;
        color: orangered;
      }
    }

    .pay-main {
      background: #fff;
      margin-top: 10px;
      border-top: 5px orange solid;
      padding: 30px;

      h4 {
        font-size: 28px;
        font-weight: normal;
        margin-bottom: 10px;
      }

      .pay-qrcode {
        padding: 0 80px;
      }

      .qrcode {
        border: 1px #ddd solid;
        padding: 15px;
        height: fit-content;

        #qrcode-stage {
          width: 200px;
          height: 200px;
          margin-bottom: 10px;
        }

        p {
          line-height: 2;
          text-align: center;
        }
      }
    }
  }
}
</style>